package yxy.game.pm2.module.camptower;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

/**
 * 阵营塔发奖执行-需求取消
 *
 * @ClassName CampTowerFutureHandle
 * @Author jingtao-zheng
 * @Date 2022/8/9 11:02
 * @Version 1.0
 **/
@Deprecated
public class CampTowerFutureHandle extends BaseGameRedisBean {
    private int type;
    private int areaId;
    private DateTime receTime;//下周领奖时间

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getAreaId() {
        return areaId;
    }

    public void setAreaId(int areaId) {
        this.areaId = areaId;
    }

    public DateTime getReceTime() {
        return receTime;
    }

    public void setReceTime(DateTime receTime) {
        this.receTime = receTime;
    }

    public CampTowerFutureHandle(int type, int areaId) {
        this.type = type;
        this.areaId = areaId;
    }
    //==================================================fanc

    /**
     * 发奖逻辑,每周发一次奖
     */
    public void handle() {
        //当前时间
        DateTime now = DateTime.now();
        //计算当前时间星期几
        int dayOfWeek = now.getDayOfWeek();
        //除了星期一凌晨的30分钟时间内的,其他时间不发奖
        if (dayOfWeek != 1) {
            return;
        }
        //当前时间的小时
        int hourOfDay = now.getHourOfDay();
        if (hourOfDay != 0) {
            return;
        }
        //当前时间的分钟
        int minuteOfHour = now.getMinuteOfHour();
        if (minuteOfHour > 30) {
            return;
        }
        CampTowerFutureHandle campTowerFutureHandle = get();

        //老时间加上7天是否和现在时间一样
        DateTime receTime = campTowerFutureHandle.getReceTime();
        DateTime dateTimeAddSeven = receTime.plusDays(7);
        String formatTime = dateTimeAddSeven.toString("yyyy-MM-dd");
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
        dateTimeAddSeven = DateTime.parse(formatTime, format);

        String formatTime2 = now.toString();
        DateTime newMillis = DateTime.parse(formatTime2, format);

        long millis = newMillis.getMillis();
        long oldMillis = dateTimeAddSeven.getMillis();
        if (millis != oldMillis) {
            return;
        } else if (millis == oldMillis) {
            //状态前置
            //当前时间加7天
            DateTime dateTime = now.plusDays(7);
            setReceTime(dateTime);
            set();
            //发奖
            //matchReward();
        }
    }




//    private void matchReward(){
//        if (getType() == Constant.WEI) {
//            weiLssueReward();
//        }
//        if (getType() == Constant.SHU) {
//            shuLssueReward();
//        }
//        if (getType() == Constant.WU) {
//            wuLssueReward();
//        }
//        if (getType() == Constant.QUN) {
//            qunLssueReward();
//        }
//    }

    private String matchRedisKey() {
        if (getType() == Constant.WEI) {
            return Constant.WEI_GUO_TA;
        }
        if (getType() == Constant.SHU) {
            return Constant.SHU_GUO_TA;
        }
        if (getType() == Constant.WU) {
            return Constant.WU_GUO_TA;
        }
        if (getType() == Constant.QUN) {
            return Constant.QUN_GUO_TA;
        }
        return null;
    }



    //==================================================redis
    private void set() {
        try {
            setv(key(), this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    private CampTowerFutureHandle get() {
        try {
            CampTowerFutureHandle handle = getv(key());
            if (handle == null) {
                //没有就初始化
                handle = createHandle();
            }
            return handle;
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    private CampTowerFutureHandle createHandle() {
        //初始化未领奖
        set();
        return get();
    }

    private String key() {
        String towerName = matchRedisKey();
        String format = String.format("AREA:%s:RANK:%s:RANKAWARD", getAreaId(), towerName);
        return format;
    }

    @Override
    public String lockKey() {
        return String.format("lock:%s", key());
    }
}
